<?php

Yii::import('application.models._base.BaseRelationship');

class Relationship extends BaseRelationship
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
    public function checkFollow($user_id,$friend_id){
        $data = $this->find('user_id=:user_id and friend_id=:friend_id and relation_status=:relation_status',array(':user_id'=>$user_id,':friend_id'=>$friend_id,':relation_status'=>1));
        if($data){
            return true;
        }else{
            return false;
        }
    }

    public function getFollowing($user_id,$my_id,$first,$total){
        $db = Yii::app()->db->createCommand();
        $db->select('u.id,u.nick_name,u.avatar_public,u.avatar_status,u.bg,u.domain');
        $db->from('{{relationship}} r');
        $db->join('{{user}} u','u.id=r.friend_id');
        if($my_id){
        $db->where('r.user_id=:user_id and r.friend_id !=:notuser and r.relation_status=:relation_status and u.status=:status',array(':user_id'=>$user_id,':notuser'=>$my_id,':relation_status'=>1,':status'=>1));
        }else{
        $db->where('r.user_id=:user_id and r.relation_status=:relation_status and u.status=:status',array(':user_id'=>$user_id,':relation_status'=>1,':status'=>1));
        }
        $db->order('r.id desc');
        $db->limit($total,$first);
        $info = $db->queryAll();
        if($info){
            foreach($info as $k=>$v){
                //获取粉丝最新的3个图片
                $info[$k]['images'] = Share::model()->getThreeShare($v['id']);
                //获取  我  与 他的粉丝的关系
                if($my_id){
                $info[$k]['relationship'] = $this->getRelationship($v['id'],$my_id);
                }else{
                $info[$k]['relationship'] = false;
                }
            }
        }
        return $info;
    }

    /**
     * 获取他的粉丝  以及他的粉丝与我的关系
     * @param $user_id  他的ID
     * @param $my_id    我的ID
     */
    public function getFollowers($user_id,$my_id,$first,$total){
        $db = Yii::app()->db->createCommand();
        $db->select('u.id,u.nick_name,u.avatar_public,u.avatar_status,u.bg,u.domain');
        $db->from('{{relationship}} r');
        $db->join('{{user}} u','u.id=r.user_id');
        if($my_id){
        $db->where('r.friend_id=:friend_id and r.user_id !=:notuser and r.relation_status=:relation_status and u.status=:status',array(':friend_id'=>$user_id,':notuser'=>$my_id,':relation_status'=>1,':status'=>1));
        }else{
        $db->where('r.friend_id=:friend_id and r.relation_status=:relation_status and u.status=:status',array(':friend_id'=>$user_id,':relation_status'=>1,':status'=>1));
        }
        $db->order('r.id desc');
        $db->limit($total,$first);
        $info = $db->queryAll();
        if($info){
            foreach($info as $k=>$v){
                //获取粉丝最新的3个图片
                $info[$k]['images'] = Share::model()->getThreeShare($v['id']);
                //获取  我  与 他的粉丝的关系
                if($my_id){
                    $info[$k]['relationship'] = $this->getRelationship($v['id'],$my_id);
                }else{
                    $info[$k]['relationship'] = false;
                }
            }
        }
        return $info;
    }
    public function meTohe($user_id,$friend_id){
        $data = $this->find('user_id=:user_id and friend_id=:friend_id',array(':user_id'=>$user_id,':friend_id'=>$friend_id));
        return $data;
    }
    public function heTome($user_id,$friend_id){
        $data = $this->find('user_id=:user_id and friend_id=:friend_id',array(':user_id'=>$friend_id,':friend_id'=>$user_id));
        return $data;
    }
    /**
     * @param $friend_id  你查看这个的用户的id
     * @param $user_id  我的id
     * @return bool
     * 查看这个用户 对于你的状态 这里   user_id = $friend_id
     */
    public function getRelationship($friend_id,$user_id){
        //你对他的关系
        $data1 = $this->meTohe($user_id,$friend_id);
        //对方对你的关系
        $data = $this->heTome($user_id,$friend_id);
        $info = array();
        $info['metohe'] = 0;
        $info['hetome'] = 0;
        if($data1){
            if($data1->relation_status == 1){
                $info['metohe'] = 1;
            }
        }
        if($data){
            if($data->relation_status == 1){
                $info['hetome'] = 1;
            }
        }
        return $info;
    }

}